/*function postJSON(url,data,callback){
	$.post(url,data,callback);
}
function getJSON(url,callback){
	$.get(url,callback);
}*/

function getJSON(url,success,error){
	$.ajax({'url':url, 'type':'GET', 'success':success, 'error':error});
}
function postJSON(url,data,success,error){
	$.ajax({'url':url, 'type':'POST', 'data':data, 'success':success, 'error':error});
}


function getAnchor(){
	return window.location.hash.replace('#','');
}

function getTime(timestamp){
	var d = new Date(timestamp * 1000);
	//addZero
	var addZero = function(obj){
		if(obj<10) return "0" +""+ obj;
		else return obj;
	}

	var str = addZero((d.getMonth()+1))+'.'+addZero(d.getDate())+' '+addZero(d.getHours())+':'+addZero(d.getMinutes());
	return str;
}

function msgbox(m){
	alert(m);
}

function rounder(num){
	return Math.round(num*100)/100;
}

/***** LoadingBox ********/
var loadingBox = {
	'timeTircker':null,
	'show':function(attr){
		var el = document.getElementById('loading-box');
		if(!el){
			var el = this.create();
		}
		this.timeTircker = window.setTimeout(function(){
			el.style.display = 'block';
		},400);
		if(attr){
			if(!attr['shadow']){
				el.classList.add('no_shadow');
			}else{
				el.classList.remove('no_shadow');
			}
		}
		el.classList.remove('hidden');
	},

	'hide':function(){
		var el = document.getElementById('loading-box');
		if(el){
			if(this.timeTircker){
				window.clearTimeout(this.timeTircker);
			}
			//el.style.display = 'none';
			el.classList.add('hidden');
		}
	},

	'create':function(){
		var newEle = document.createElement('div');
		newEle.className = 'loading-box';
		newEle.id = 'loading-box';
		newEle.innerHTML = '<div class="block"></div>';
		newEle.style.display = 'none';
		document.body.appendChild(newEle);
		return newEle;
	},
}

var msgBox = {//msgbox 不可以有多个同时出现
	'show':function(h,p){
		var el = document.getElementById('msgbox');
		if(el){
			el.parentNode.removeChild(el);
		}
		var el = this.create({'title':h,'content':p});
		el.offsetWidth;
		el.classList.remove('hidden');
		//el.style.display = 'block';
	},

	'pop':function(para){
		var el = document.getElementById('msgbox');
		if(el){
			el.parentNode.removeChild(el);
		}
		var el = this.create(para);
		el.offsetWidth;
		el.classList.remove('hidden');
		if(para['noShadow']){
			el.classList.add('no_shadow');
		}else{
			el.classList.remove('no_shadow');
		}
		//
		disableBodyScroll();
	},

	'hide':function(){
		var el = document.getElementById('msgbox');
		if(el){
			el.classList.add('hidden');
			//el.parentNode.removeChild(el);
		}
		enableBodyScroll();
	},

	'create':function(cont){
		var h = cont['title'], p = cont['content'], icon = cont['icon'] || 'information';
		var newEle = document.createElement('div');
		newEle.className = 'msgbox hidden';
		newEle.id = 'msgbox';

		var html = '<div class="block">';
		if(cont['close']){
			html += '<span class="close-btn" onclick="msgBox.hide()"></span>';
		}
		html += '<div class="icon '+icon+'"></div><h3>'+h+'</h3><p class="main">'+p+'</p>';

		//bottom button
		if(cont['buttons'] && cont['buttons'].length){
			for(var i=0; i<cont['buttons'].length; i++){
				var curBtn = cont['buttons'][i];
				if(curBtn.link){
					html += '<a class="highlight-btn" href="'+curBtn.link+'">'+curBtn.text+'</a>';
				}else if(curBtn.action){
					html += '<a class="highlight-btn" href="javascript:;" onclick="'+curBtn.action+'">'+curBtn.text+'</a>';
				}
			}
		}

		newEle.innerHTML = html+'</div>';
		document.body.appendChild(newEle);
		return newEle;
	},
}

//Stop <Body> Scroll
function disableBodyScroll(){
	document.body.classList.add('scroll-forbidden');
}
function enableBodyScroll(){
	document.body.classList.remove('scroll-forbidden');
}

/**** Hash Query Decoder and Encoder ****/

function getQuery(){
	return hashQueryDecode(location.hash);
}

function hashQueryEncode(paras){
	var queryStr = '', pushStr ='' ;
	var isFirst = true;
	for(paraName in paras){
		pushStr = '';
		if(!isFirst){
			pushStr += '&';
		}
		pushStr += (paraName + '=' + paras[paraName]);
		queryStr += pushStr;
	}
	return queryStr;
}
function hashQueryDecode(queryStr){
	var temp = [], sr, queryParas={};
	temp = queryStr.split('&');
	for(var i=0; i<temp.length; i++){
		sr = temp[i].split('=');
		queryParas[sr[0]] = sr[1];
	}
	return queryParas;
}

//对poll id  进行加密解密的函数
var idNumberCoder = {
	'key':56,
	'encode':function(num){
		var oct = parseInt(num);
		var rh  = oct ^ this.key;
		var hex = parseInt(num).toString(16);
		return rh;
	},
	'decode':function(hex){
		var rh = parseInt(hex,16);
		var oct = hex ^ this.key;
		return oct;
	},
}




/***** APIURL & Other static data ********/
if(location.href.indexOf('yishun')!=-1){
	var API_URL = 'http://wechatvote.yishun.co/vote/';
}else{
	var API_URL = 'http://127.0.0.1/vote/';
}



/******** KEY *******/

	(function(){
		if(location.href.indexOf('yishun')!=-1){
			var RELOCATE_URL = 'http://wechatvote.yishun.co/wechat/check/';
		}else{
			var RELOCATE_URL = 'http://127.0.0.1/wechat/check/';
		}

		postJSON(RELOCATE_URL,{'target_url':location.href},function(data){
			var redirectUrl = data['redirect_uri'];
			if(redirectUrl.indexOf('authorize') != -1){
				//console.log(redirectUrl);
				location.assign(redirectUrl);
			}
		});
	})();






